import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import type {
  TenantAddItem, TenantChangeBelongBean, TenantItem, TenantUpdateItem,
} from './data.d';
import { message } from 'antd';
import { request } from '@/.umi/plugin-request/request';

//获取简单租户列表
export async function getTenantList(params: {
  page?: number;
  pageSize?: number;
}): Promise<ResponseData<any>> {
  const res = await request('/tenant/admin_simple_view', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (res) return { code: res.code, data: res.data?.list, success: true, message: '' };
  return res;
}

/**
 * 管理员获取客户列表
 * @param params
 */
export async function queryTenantListByAdmin(params: {
  page?: number,
  pageSize?: number,
}
): Promise<RequestData<TenantItem>> {
  try {
    const res: ResponsePaginationData<TenantItem> = await request('/tenant/admin_view', {
      params: {
        page: 1,
        pageSize: 10,
        ...params,
      },
    });
    if (res?.code !== SUCCESS_CODE) {
      return { success: false, total: 0, data: [] };
    }
    return {
      success: true,
      total: res.data.total,
      data: res.data.list,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

/**
 * 创建一个客户
 * @param data
 */
export async function addTenant(data: TenantAddItem) {
  try {
    const res = await request('/tenant/add', {
      method: 'POST',
      data: data,
    });
    if (!res || res.code !== SUCCESS_CODE) {
      message.error(res.message);
    }
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

/**
 * 修改客户信息
 * @param data
 */
export async function updateTenant(data: TenantUpdateItem) {
  try {
    const res = await request('/tenant/update', {
      method: 'POST',
      data: data,
    });
    if (!res || res.code !== SUCCESS_CODE) {
      message.error(res.message);
    }
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: null };
  }
}

/**
 * 删除客户
 * @param id 
 * @returns 
 */
export async function deleteTenant(id: string): Promise<ResponseData<any>> {
  const res = await request('/tenant/delete', {
    method: 'POST',
    data: {
      id: id
    },
  });
  if (res) return { code: res.code, data: res.data, success: true, message: res.message };
  return res;
}

/**
 * 修改客户权限
 * @param data 
 * @returns 
 */
export async function changeTenantRole(data: any): Promise<ResponseData<any>> {
  const res = await request('/tenant/change_role', {
    method: 'POST',
    data: {
      tenant_id: data.tenant_id,
      role_id: data.role_id
    },
  });
  if (res.code !== SUCCESS_CODE) return { code: res.code, data: res.data, success: false, message: res.message };
  return res;
}

/**
 * 修改客户所属权
 * @param data 
 * @returns 
 */
export async function changeTenantBelong(data: TenantChangeBelongBean): Promise<ResponseData<any>> {
  const res = await request('/admin/tenant/edit_belong', {
    method: 'POST',
    data: data,
  });
  if (res.code !== SUCCESS_CODE) return { code: res.code, data: res.data, success: false, message: res.message };
  return res;
}
